package com.leantech.android.browser.ui.shortcut.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import coil.load
import com.cscj.android.repository.db.entity.ShortCutEntity
import com.leantech.android.browser.databinding.ItemLinkBinding
import com.leantech.android.browser.utils.onThrottleClick

class LinkItemAdapter(private val linkItemCallback: LinkItemCallback) :
    ListAdapter<ShortCutEntity, LinkItemAdapter.LinkItemViewHolder>(diffCallback) {

    companion object {

        private const val PayloadAdded = 1

        private val diffCallback = object : DiffUtil.ItemCallback<ShortCutEntity>() {
            override fun areItemsTheSame(
                oldItem: ShortCutEntity, newItem: ShortCutEntity
            ): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(
                oldItem: ShortCutEntity, newItem: ShortCutEntity
            ): Boolean {
                return oldItem == newItem
            }

            override fun getChangePayload(oldItem: ShortCutEntity, newItem: ShortCutEntity): Any? {
                if (oldItem.added != newItem.added) {
                    return PayloadAdded
                }
                return super.getChangePayload(oldItem, newItem)
            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LinkItemViewHolder {
        val binding = ItemLinkBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return LinkItemViewHolder(binding)
    }

    override fun onBindViewHolder(holder: LinkItemViewHolder, position: Int) {
        holder.bind(getItem(position), linkItemCallback)
    }

    override fun onBindViewHolder(
        holder: LinkItemViewHolder, position: Int, payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            payloads.forEach { payload ->
                holder.onPayload(getItem(position), payload)
            }
        }

    }

    interface LinkItemCallback {
        fun onLinkItemAdd(shortCut: ShortCutEntity)
    }

    class LinkItemViewHolder(private val binding: ItemLinkBinding) : ViewHolder(binding.root) {

        fun bind(data: ShortCutEntity, linkItemCallback: LinkItemCallback) {
            binding.cover.load(data.icon)
            binding.title.text = data.name
            binding.btnAdd.isSelected = data.added
            binding.btnAdd.onThrottleClick {
                val selected = it.isSelected
                if (!selected) { //
                    linkItemCallback.onLinkItemAdd(data)
                }
            }
        }

        fun onPayload(data: ShortCutEntity, payload: Any) {
            if (payload == PayloadAdded) {
                binding.btnAdd.isSelected = data.added
            }
        }

    }
}